package lsl.leetCode.year2023.test0205;

/**
 * 202.快乐数---快慢指针
 *
 * @author Zed
 * @description
 * @date 2023/2/5 20:49
 */
public class test202 {
    public static void main(String[] args) {
        System.out.println(isHappy(2));
    }

    public static boolean isHappy(int n) {
        int slowRunner = n;
        int fastRunner = getNext(n);
        while (slowRunner != fastRunner && fastRunner != 1) {
            slowRunner = getNext(slowRunner);
            fastRunner = getNext(getNext(fastRunner));
        }
        return fastRunner == 1;
    }

    public static int getNext(int n) {
        int sum = 0;
        while (n != 0) {
            int num = n % 10;
            num = num * num;
            sum += num;
            n = n / 10;
        }
        return sum;
    }
}
